#!/bin/bash

if [ -z "$1" ]; then
    echo "usage: $0 <project_folder_path>"
    exit 1
fi

project_folder=$(realpath "$1")

total=$(find "$project_folder" -name "*.h" -o -name "*.cc" | wc -l)
count=0

find "$project_folder" -name "*.h" -o -name "*.cc" | while read -r file; do
    ((count++))
    echo "[$count/$total] formatting $file"
    clang-format -i -style="{BasedOnStyle: Google, Language: Cpp, Standard: Latest, AccessModifierOffset: -2, AlignArrayOfStructures: Left, AlignAfterOpenBracket: Align, AlignConsecutiveAssignments: true, AlignConsecutiveDeclarations: true, AlignOperands:  true, AlignTrailingComments: true, AllowShortBlocksOnASingleLine: true, AllowShortEnumsOnASingleLine: true, AllowShortFunctionsOnASingleLine: Inline, AllowShortIfStatementsOnASingleLine: AllIfsAndElse, AllowShortLambdasOnASingleLine: All, AlwaysBreakTemplateDeclarations: Yes, BinPackArguments: true, BreakBeforeBraces: Custom, BraceWrapping:{BeforeElse: true, IndentBraces: false}, BreakConstructorInitializers: BeforeComma, ColumnLimit: 180, ConstructorInitializerIndentWidth: 8, ContinuationIndentWidth: 4, DerivePointerAlignment: false, FixNamespaceComments: true, IndentWidth: 4, InsertBraces: false, RemoveBracesLLVM: true, RemoveParentheses: ReturnStatement, KeepEmptyLinesAtTheStartOfBlocks: false, MaxEmptyLinesToKeep: 1, NamespaceIndentation: None, PointerAlignment: Left, ReflowComments: false, SpaceAfterLogicalNot: true, SpacesBeforeTrailingComments: 1, SpacesInAngles: false, SpacesInParentheses: false, SpacesInSquareBrackets: false, TabWidth: 4, UseTab: Never}" "$file"
done